<?php

App::uses('Security', 'Utility');

class AuthresourcesController extends AppController {

    var $helpers = array('Html', 'Form');
    var $uses = array('Authresource', 'Authpermission');
    var $paginate = array('limit' => 50, 'order' => array('Authresource.authresourcegroup_id' => 'asc', 'Authresource.description' => 'asc'));

    function index() {
        $this->set('menu', 'users');
        $this->set('submenu', 'resources');
        $this->set('cakeDescription', 'Resources');
        $this->set('title_for_layout', 'Index');

        $textsearch = "";
        if (!empty($this->data)) {
            $textsearch = $this->data['Authresource']['textsearch'];
            $this->paginate['conditions'] = array('or' => array('Authresource.description LIKE' => '%' . $textsearch . '%',
                    'Authresource.name LIKE' => '%' . $textsearch . '%'));
        } else {
            if (isset($this->params['named']['textsearch'])) {
                $textsearch = $this->params['named']['textsearch'];
                $this->paginate['conditions'] = array('or' => array('Authresource.description LIKE' => '%' . $textsearch . '%',
                        'Authresource.name LIKE' => '%' . $textsearch . '%'));
            }
        }
        $this->Authresource->recursive = 0;
        $this->set('authresources', $this->paginate());
        $this->set('textsearch', $textsearch);
    }

    function view($id = null) {
        $this->set('menu', 'users');
        $this->set('submenu', 'resources');
        $this->set('cakeDescription', 'Resources');
        $this->set('title_for_layout', 'View');
        if (!$id) {
            $this->Session->setFlash(__('Invalid Authresource', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('authresource', $this->Authresource->read(null, $id));
    }

    function add() {
        $this->set('menu', 'users');
        $this->set('submenu', 'resources');
        $this->set('cakeDescription', 'Resources');
        $this->set('title_for_layout', 'Add');
        if (!empty($this->data)) {
            $this->Authresource->create();
            if ($this->Authresource->save($this->data)) {
                $this->Session->setFlash(__('The Authresource has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Authresource could not be saved. Please, try again.', true));
            }
        }
        $this->Authresource->Authusergroup->recursive = -1;
        $authusergroups = $this->Authresource->Authusergroup->find('list');
        $this->Authresource->Authresourcegroup->recursive = -1;
        $authresourcegroups = $this->Authresource->Authresourcegroup->find('list');
        $this->set(compact('authusergroups', 'authresourcegroups'));
    }

    function edit($id = null) {
        $this->set('menu', 'users');
        $this->set('submenu', 'resources');
        $this->set('cakeDescription', 'Resources');
        $this->set('title_for_layout', 'Edit');
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Authresource', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Authresource->save($this->data)) {
                $this->Session->setFlash(__('The Authresource has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Authresource could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Authresource->read(null, $id);
        }
        $this->Authresource->Authusergroup->recursive = -1;
        $authusergroups = $this->Authresource->Authusergroup->find('list');
        $this->Authresource->Authresourcegroup->recursive = -1;
        $authresourcegroups = $this->Authresource->Authresourcegroup->find('list');
        $this->set(compact('authusergroups', 'authresourcegroups'));
    }

    function delete($id = null) {
        Configure::write('debug', 0);
        if (!$id) {
            $this->Session->setFlash(__('Invalid Authresource', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Authresource->delete($id)) {
            $this->Session->setFlash(__('Authresource deleted', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('not deleted', true));
            $this->redirect(array('action' => 'index'));
        }
    }

}

?>